package cn.chencaiju.algorithm;

public class Reverser {
	public static void main(String[] args) {
		System.out.println(reverse(-2147483648));
	}
    public static int reverse(int x) {
    	boolean flag=false;
    	long X=x;
    	if(X<0) {
    		X=-X;
    		flag=true;
    	}
    	long temp=0;
    	long result=0;
    	while(true) {
    		temp=X/10;
    		if(temp==0) {
    			result=result*10;
    			result+=X;
    			if(result>Math.pow(2, 31)-1)
    				return 0;
    			if(!flag) {
    				return (int)result;
    			}
    			return -(int)result;
    		}else {
    			temp=X%10;
    			result=result*10+temp;
    			X/=10;
    		}
    	}
    }	
}
